package com.cz.video.website.controller;


import com.cz.video.common.annotation.Log;
import com.cz.video.common.constant.ClientConstants;
import com.cz.video.common.core.controller.BaseController;
import com.cz.video.common.core.domain.PageQuery;
import com.cz.video.common.core.domain.R;
import com.cz.video.common.core.domain.entity.SysUser;
import com.cz.video.common.core.page.TableDataInfo;
import com.cz.video.common.enums.BusinessType;
import com.cz.video.common.exception.ServiceException;
import com.cz.video.common.helper.LoginHelper;
import com.cz.video.website.domain.bo.WebsiteFollowBo;
import com.cz.video.website.domain.vo.WebsiteFollowVo;
import com.cz.video.website.service.WebsiteFollowService;
import com.cz.video.system.api.RemoteUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Objects;

/**
 * <p>
 * 关注信息 前端控制器
 * </p>
 *
 */
@Validated
@Tag(name = "关注管理")
@RequiredArgsConstructor
@RestController
@RequestMapping("/client/follow")
public class WebsiteFollowController extends BaseController {

  private final WebsiteFollowService followService;
  @DubboReference
  private final RemoteUserService userService;


  /**
   * 分页获取关注信息
   *
   * @param followBo 查询参数
   * @param pageQuery 分页参数
   *
   * @return 分页关注信息
   */
  @Operation(summary = "分页获取关注信息")
  @GetMapping("/list")
  public TableDataInfo<WebsiteFollowVo> list(WebsiteFollowBo followBo, PageQuery pageQuery) {
    return followService.page(followBo, pageQuery);
  }

    /**
     * 分页获取关注信息
     *
     * @return 分页关注信息
     */
    @Operation(summary = "关注或拉黑")
    @PostMapping
    public R<Void> follow(@Parameter(description = "用户id") @NotNull(message = "没有选择关注的用户") Long userId,
                          @Parameter(description = "(0:关注,1: 拉黑)") @NotNull(message = "没有选择是否关注") Integer type) {

        if (Objects.equals(LoginHelper.getUserId(), userId)) {
            if (ClientConstants.FOLLOW_TYPE==type)
                throw new ServiceException("不可以关注自己");
            else if (ClientConstants.BLACK_FOLLOW_TYPE==type)
                throw new ServiceException("不可以拉黑自己");
            else throw new ServiceException("类型不正确");
        }
        SysUser userVo = userService.selectUserById(userId);
        if (userVo == null) {
            throw new ServiceException("该用户不存在");
        }
        WebsiteFollowBo followBo = new WebsiteFollowBo();
        if (ClientConstants.FOLLOW_TYPE!= type&&ClientConstants.BLACK_FOLLOW_TYPE!= type){
            throw new ServiceException("关注的类型不正确");
        }
        followBo.setFollowerId(LoginHelper.getUserId()).setFollowingId(userId).setType(type);
        return toAjax(followService.add(followBo));
    }

  /**
   * 删除关注
   *
   * @param ids 关注
   *
   * @return 影响行数
   */
  @Log(title = "删除关注或拉黑", businessType = BusinessType.DELETE)
  @Operation(summary = "删除关注或拉黑")
  @DeleteMapping
  public R<Void> remove(@NotEmpty(message = "未选择要取消关注的用户列表") Long[] ids) {
    return toAjax(followService.deleteByUserIds(ids));
  }

}
